import { ApiProperty } from '@nestjs/swagger';
import {
  Column,
  CreateDateColumn,
  Entity,
  ManyToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Role } from '../role/role.entity';

@Entity()
export class Menu {
  @ApiProperty({ type: Number, description: 'id' })
  @PrimaryGeneratedColumn()
  public id: number;

  @ApiProperty({ type: Number, description: '父Id' })
  @Column({
    type: 'int',
    comment: '父Id',
  })
  public parent_id: number;

  @ApiProperty({ type: String, description: '菜单名称' })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '菜单名称',
  })
  public title: string;

  @ApiProperty({ type: String, description: '菜单路径' })
  @Column({
    type: 'varchar',
    comment: '菜单路径',
    nullable: true,
  })
  public path: string;

  @ApiProperty({ type: String, description: '组件名称' })
  @Column({
    type: 'varchar',
    comment: '组件名称',
  })
  public name: string;

  @ApiProperty({ type: String, description: '组件' })
  @Column({
    type: 'varchar',
    comment: '组件',
    nullable: true,
  })
  public component: string;

  @ApiProperty({ type: String, description: '是否外链(1是，0否)' })
  @Column({
    type: 'varchar',
    comment: '是否外链(1是，0否)',
    nullable: true,
    default: '0',
  })
  public is_frame: string;

  @ApiProperty({ type: String, description: '菜单类型（M目录，C菜单，F按钮）' })
  @Column({
    type: 'varchar',
    comment: '菜单类型（M目录，C菜单，F按钮）',
  })
  public menu_type: string;

  @ApiProperty({ type: String, description: '菜单显示状态(1: 显示, 0: 隐藏)' })
  @Column({
    type: 'varchar',
    comment: '菜单显示状态',
    nullable: true,
    default: '1',
  })
  public visible: string;

  @ApiProperty({ type: Number, description: '显示顺序' })
  @Column({
    type: 'int',
    comment: '显示顺序',
    default: 1,
  })
  public sort: number;

  @ApiProperty({ type: String, description: '菜单状态（1正常，0停用）' })
  @Column({
    type: 'varchar',
    comment: '菜单状态（1正常，0停用）',
    nullable: true,
    default: '1',
  })
  public status: string;

  @ApiProperty({ type: String, description: '是否缓存（1缓存，0不缓存）' })
  @Column({
    type: 'varchar',
    comment: '是否缓存（1缓存，0不缓存）',
    nullable: true,
    default: '0',
  })
  public keep_alive: string;

  @ApiProperty({ type: String, description: '权限标识' })
  @Column({
    type: 'varchar',
    comment: '权限标识',
    nullable: true,
  })
  public perms: string;

  @ApiProperty({ type: String, description: '图标' })
  @Column({
    type: 'varchar',
    comment: '图标',
    nullable: true,
  })
  public icon: string;

  @ApiProperty({
    type: String,
    description: '备注',
  })
  @Column({
    type: 'text',
    comment: '备注',
    nullable: true,
  })
  public remark: string;

  @ApiProperty({ type: Date, description: '创建时间' })
  @CreateDateColumn({
    type: 'timestamp',
    comment: '创建时间',
    nullable: true,
  })
  public created_date: Date;

  @ApiProperty({
    type: String,
    description: '创建人',
  })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '创建人',
    nullable: true,
  })
  public created_by: string;

  @ApiProperty({ type: Date, description: '更新时间' })
  @UpdateDateColumn({
    type: 'timestamp',
    comment: '更新时间',
    nullable: true,
  })
  public updated_date: Date;

  @ApiProperty({
    type: String,
    description: '更新人',
  })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '更新人',
    nullable: true,
  })
  public updated_by: string;

  @ApiProperty({ type: Array, description: '角色' })
  @ManyToMany(() => Role, (role) => role.menus, {
    createForeignKeyConstraints: false, //避免创建外键约束
  })
  roles: Role[];

  constructor(partial: Partial<Menu>) {
    Object.assign(this, partial);
  }
}
